// miniprogram/pages/rule/list.js
Page({
  data: {
    rules: [],
    sceneType: '',
    sceneTypeName: ''
  },

  onLoad() {
    const app = getApp()
    this.setData({
      sceneType: app.globalData.sceneType,
      sceneTypeName: app.getSceneTypeName(app.globalData.sceneType)
    })
    this.loadRules()
  },

  onShow() {
    this.loadRules()
  },

  loadRules() {
    // 模拟数据
    const rules = [
      { id: 1, name: '完成家务', points: 10, description: '完成指定家务任务' },
      { id: 2, name: '早睡早起', points: 5, description: '晚上11点前睡觉，早上7点前起床' },
      { id: 3, name: '运动健身', points: 15, description: '完成30分钟以上运动' },
      { id: 4, name: '迟到', points: -5, description: '每迟到一次扣分' },
      { id: 5, name: '忘记重要事情', points: -10, description: '忘记重要纪念日或任务' }
    ]
    
    this.setData({ rules })
  },

  addRule() {
    wx.navigateTo({
      url: '/pages/rule/add'
    })
  },

  editRule(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/rule/add?id=${id}`
    })
  },

  deleteRule(e) {
    const id = e.currentTarget.dataset.id
    wx.showModal({
      title: '确认删除',
      content: '确定要删除这条积分规则吗？',
      success: (res) => {
        if (res.confirm) {
          // 模拟删除操作
          wx.showToast({
            title: '删除成功',
            icon: 'success'
          })
          this.loadRules()
        }
      }
    })
  }
})